#include "QNode.h"


void Queueinit(Queue* ps)
{
	assert(ps);
	ps->head = ps->tail = NULL;
	ps->size = 0;
}
void QueueDestroy(Queue* ps)
{
	assert(ps);
	Qnode* cur = ps->head;
	while (cur)
	{
		Qnode* next = cur->next;
		free(cur);
		cur = next;
	}
	ps->head = ps->tail = NULL;
	ps->size = 0;
}
void Queuepush(Queue* ps, QDataType x)
{
	Qnode* cur = (Qnode*)malloc(sizeof(Qnode));
	if (cur == NULL)
	{
		perror("malloc fail");
		return;
	}

	cur->data = x;
	cur->next = NULL;
	if (ps->head == NULL)
	{
		assert(ps->tail == NULL);
		ps->head = ps->tail = cur;
	}
	else
	{
		ps->tail->next = cur;
		ps->tail = cur;
	}
	ps->size++;
}
void Queuepop(Queue* ps)
{
	assert(ps);
	assert(ps->head != NULL);
	Qnode* newnode = ps->head;
	if (ps->head->next == NULL)
	{
		free(ps->head);
		ps->head = ps->tail = NULL;
	}
	else
	{
		ps->head = ps->head->next;
		free(newnode);
	}
	ps->size--;
}
int Queuesize(Queue* ps)
{
	assert(ps);
	return ps->size;
}
bool QueueEmpty(Queue* ps)
{
	assert(ps);
	return ps->head == NULL;
}
QDataType QueueBack(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	return ps->tail->data;
}
QDataType QueueFront(Queue* ps)
{
	assert(ps);
	assert(!QueueEmpty(ps));
	return ps->head->data;
}